qui(function(require, exports, module) {
    Server.ClusterList = function(options) {
        this.dao = Server.Dao;
        this.options = $.extend(true, {}, options);
        this.util = Server.Util || {};
        this.config = Server.config || {};
    };

    Com.inherits(Server.ClusterList, Ancestor.Grid).extend({
        run: function(options, callback) {
            var self = this;
            var $container = self.options.$container;
            var gridConfig = self.getGridConfig(Server.Config.TABLE_SERVER_CLUSTER,Server.Config.TABLE_SERVER_CLUSTER_COLMODEL_CONFIG);

            self.$grid = $container.find('table.grid');
            //self.util.getClusterList($container);
            options = $.extend(true, {
                width: 1000,
                shrinkToFit: false,
                autowidth: false,
                subGrid:true,

                subGridRowExpanded: function(subgrid_id, row_id) {
                    var subgrid_table_id;
                    subgrid_table_id = subgrid_id + "_t";

                    var subgrid_pager_id;
                    subgrid_pager_id = subgrid_id + "_pgr"

                    // (5)动态添加子报表的table和pager
                    $("#" + subgrid_id).html("<table id='"+subgrid_table_id+"'class='scroll'></table></div>");

                    // (6)创建jqGrid对象
                    $("#" + subgrid_table_id).jqGrid({
                        url:"/api/sginal/server/clusterSeverList",  //(7)子表格数据对应的url，注意传入的contact.id参数
                        datatype: "json",

                        loadBeforeSend:function(xhr, settings){
                            var auth = self.util.getAuth("/sginal/server/clusterSeverList");
                            xhr.setRequestHeader('Sign',auth.sign);
                            xhr.setRequestHeader("Time",auth.clientTime);
                            xhr.setRequestHeader("From",auth.from);
                            return true;
                        },
                        postData:{'cluster':row_id},

                        colNames: ['IP','收录','最大收录'],
                        colModel: [
                            {name:"insideServerIp",index:"insideServerIp",width:150,sortable:false, align:'center'},
                            {name:"now_including",index:"now_including",width:150,sortable:false, align:'center'},
                            {name:"including",index:"including",width:150,sortable:'false', align:'center'},
                        ],
                        mtype: "post",
                        prmNames: {search: "search"},

                        height: "100%",
                        rowNum: 200,
                        width:700,
                        shrinkToFit: true
                    });
                },

            }, gridConfig, options);
            self.$grid.jqGrid(options);
			self.perpareData();
            self.bindEvent();
            self.initSuggest();
        },

        perpareData: function() {
            var self = this;
            var username = $("div.user a.name").html();
        },

        bindEvent: function() {
            var self = this;
            var $container = self.options.$container;
            var $search = $container.find('.search');
            var $new = $container.find('.edit');

            $search.on('click', function() {
                self.query();
            });

            $new.on('click', function() {
                window.open("edit_cluster.html");
            });
        },

        query: function() {
            var self = this;
            var $container = self.options.$container;
            var $grid = self.$grid;
            var param = self.getParam();
            $grid.jqGrid('setGridParam', {
                postData: param,
            }).trigger('reloadGrid', [{
                page: 1
            }]);
        },

        getParam:function(){
            var self = this;
            var $container = self.options.$container;
            var $input = $container.find('input');
            return {'clusterName':$input.val().trim()};
        },
        jumpFromHomePage: function(){
            var url = window.location.href;
            if(url.indexOf('?')==-1){
                return;
            }
            var id = url.split('?')[1];
           self.dao.clusterList({'id':id},function(rdata){
               var res = rdata['data'][result];
           });
        },

        initSuggest: function () {
            var self = this;
            var ClusterSuggest = {
                options: {
                    ajaxSettings: {
                        beforeSend: function (request) {
                            var authBase = Ancestor.Util.getAuth("/sginal/server/clusterList");
                            request.setRequestHeader('Sign', authBase.sign);
                            request.setRequestHeader("Time", authBase.clientTime);
                            request.setRequestHeader("From", authBase.from);
                            request.setRequestHeader("UserName", (auth.baseInfo.authInfo == null) ? "" : escape(auth.baseInfo.authInfo.userName).toLocaleLowerCase().replace(/%u/gi, '\\u'));
                        }
                    },
                    select: function (event, object) {
                        var obj = { "clusterName": Number(object.item.id) };
                        return true;
                    },
                    source: "/api/sginal/server/clusterList",
                    method: "post",
                    autoFilter: false,
                    autoFocus: false,
                    multiple: false,
                    parseValue: function () {
                        var val = this.element.val(),
                            ret = []
                        return ret;
                    },
                    getParam: function (term) {
                        return { "clusterName": $.trim(term) }
                    },
                    process: function (json) {
                        var ret = [],
                            self = this,
                            options = self.options,
                            itemData = null;
                        var data = null;
                        if (json == null) {
                            return [];
                        }
                        if (json.code === "A00000") {
                            data = json.data == null ? null : json.data;
                        }
                        if (!data) {
                            return [];
                        }
                        $.map(data.result, function (item) {
                            itemData = {};
                            itemData[options.valueKey] = item["id"];
                            itemData[options.labelKey] = item["name"]
                            ret.push(itemData);
                        });
                        return ret;
                    },
                    template: function (item) {
                        return item.label;
                    }
                }
            }
            ClusterSuggest.options.clearInvalid = false;
            $("input[name=clusterId]").suggest(ClusterSuggest.options);

        }

    });

});
